package com.bwie.filter;

import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.concurrent.TimeUnit;

/**
 * @Author: TaoShiHao
 * @Date: 2025/4/22 19:30
 * @Description:
 **/
@Configuration
@Order(1)
public class MyGlobalFilter implements GlobalFilter {

    @Autowired
    StringRedisTemplate redisTemplate;

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 获取请求参数
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        String url = request.getURI().getPath();
        System.out.println("请求路径:" + url);

        //白名单
        //登录 注册 发送验证码
        if (url.contains("/adminLogin")) {
            System.out.println("登录接口白名单:" + url);
            //放行
            return chain.filter(exchange);
        }
        //注册
        if (url.contains("/userReg")) {
            System.out.println("注册接口白名单:" + url);
            //放行
            return chain.filter(exchange);
        }

        //检查并且验证token
        //获取请求头的token
        String token = request.getHeaders().getFirst("token");
        if (token == null || token.isEmpty()) {
            System.out.println("token为空:" + url);
            //返回错误码401表示没有认证
            //token为空 进行拦截 返回错误码
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }
        //获取redis里面的token
        Integer userId = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userId", Integer.class);
        String redisKey = "token-" + userId;
        String redisToken = redisTemplate.opsForValue().get(redisKey);

        //如果token为空
        if (redisToken == null || redisToken.isEmpty()) {
             //返回错误码401表示没有认证
            //token为空 进行拦截 返回错误码
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }
        //比较请求头和redis的token
        if(!token.equals(redisToken)){
            System.out.println("token错误"+url);
            //返回错误码401表示没有认证
            //token为空 进行拦截 返回错误码
            response.setStatusCode(HttpStatus.UNAUTHORIZED);
            return response.setComplete();
        }
        //token续期  续期60分钟
        redisTemplate.expire(redisKey,60, TimeUnit.MINUTES);


        //放行(转发)
        System.out.println("放行:"+url);
        return chain.filter(exchange);
    }
}
